package com.yeyks.controller.member;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.dto.member.MemberCardSellRelationWebDTO;
import com.yeyks.common.param.member.MemberCardSellRelationDownQueryParam;
import com.yeyks.common.param.member.MemberCardSellRelationQueryParam;
import com.yeyks.common.result.member.MemberCardSellRelationWebResult;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.consumer.MemberCardService;
import com.yeyks.commonReference.service.vip.MemberCardEnableOrderService;
import com.yeyks.consumer.dal.domain.MemberCard;
import com.yeyks.vip.dal.domain.MemberCardEnableOrder;
import com.yeyks.vip.dal.domain.MemberCardSellRelation;
import com.yeyks.vip.service.MemberCardSellRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: dagny
 * @date: 2019/12/24 0024 15:28
 */
@Api(tags = "V3.8.3优享VIP卡订单")
@RestController
@RequestMapping("/member/vip/order")
public class MemberCardSellRelationController {


    @Autowired
    private MemberCardSellRelationService memberCardSellRelationService;

    @Autowired
    private MemberCardService memberCardService;

    @Autowired
    private MemberCardEnableOrderService memberCardEnableOrderService;

    @Autowired
    private AccountInfoService accountInfoService;

    @NeedToken
    @ApiOperation(value = "V3.8.3分页查询【优享VIP卡订单】", response = MemberCardSellRelationWebDTO.class)
    @PostMapping("/query")
    public AjaxResponseBody queryPage(@Validated MemberCardSellRelationQueryParam param){

        AjaxResponseBody body = null;
        List<MemberCardSellRelation> memberCardSellRelationList = memberCardSellRelationService.queryByParam(param);
        int count = memberCardSellRelationService.queryCount(param);
        HashMap<String, Object> map = Maps.newHashMap();
        List<MemberCardSellRelationWebDTO> list = ObjectConvertUtil.convertList(memberCardSellRelationList, MemberCardSellRelationWebDTO.class);
        convertResultList(list);
        map.put("list", list);
        map.put("count", count);
        body = AjaxResponseBody.success(map);
        return body;
    }

    @CrossOrigin
    @ApiOperation(value = "V4.1.0导出优享VIP卡订单", notes = "V4.1.0导出优享VIP卡订单")
    @PostMapping(value = "/downMemberCardSellRelation")
    public void downMemberCardSellRelation(@Validated MemberCardSellRelationDownQueryParam param, HttpServletResponse response) {

        List<MemberCardSellRelation> memberCardSellRelationList = memberCardSellRelationService.queryDownLoad(param);
        List<MemberCardSellRelationWebResult> relationWebResults = Lists.newArrayList();
        List<MemberCardSellRelationWebDTO> list = ObjectConvertUtil.convertList(memberCardSellRelationList, MemberCardSellRelationWebDTO.class);
        convertResultList(list);
        if(!CollectionUtils.isEmpty( list )){
            list.forEach( memberCardSellRelationWebDTO -> {
                MemberCardSellRelationWebResult result = new MemberCardSellRelationWebResult();
                result.setOrderNo( memberCardSellRelationWebDTO.getOrderNo());
                result.setCardNo( memberCardSellRelationWebDTO.getCardNo());
                result.setCreateTime( memberCardSellRelationWebDTO.getCreateTime());
                result.setName( memberCardSellRelationWebDTO.getName());
                result.setPhone( memberCardSellRelationWebDTO.getPhone());
                result.setPhoneSell( memberCardSellRelationWebDTO.getPhoneSell());
                result.setSelaName( memberCardSellRelationWebDTO.getSelaName());
//                //取订单的价格，并不一定是会员卡的销售价
//                MemberCardEnableOrder memberCardEnableOrder = memberCardEnableOrderService.getOne(new QueryWrapper<MemberCardEnableOrder>().eq(MemberCardEnableOrder.ORDER_NO,memberCardSellRelationWe.bDTO.getOrderNo()));
//                if (null == memberCardEnableOrder ||null ==memberCardEnableOrder.getPayValue()){
//                    memberCardSellRelationWebDTO.setOrderPrice(0);
//                }else {
//                    memberCardSellRelationWebDTO.setOrderPrice(memberCardEnableOrder.getPayValue());
//                }
                if(null != memberCardSellRelationWebDTO.getOrderPrice()){
                    result.setOrderPrice( String.valueOf((Double.parseDouble(memberCardSellRelationWebDTO.getOrderPrice().toString())/100) ) );
                }
                if(memberCardSellRelationWebDTO.getStatus() == 1){
                    result.setStatus( "待支付" );
                }else{
                    result.setStatus( "已支付" );
                }
                if(memberCardSellRelationWebDTO.getType() == 1){
                    result.setType( "线下" );
                }else {
                    result.setType( "线上" );
                    result.setName("");
                }
                relationWebResults.add(result);
            } );
            ExcelUtil.disposeWriteExcelWithSheets(response, relationWebResults, new MemberCardSellRelationWebResult());
        }
    }

    /**
     * 参数补充
     * @param list 集合信息
     */
    private void convertResultList(List<MemberCardSellRelationWebDTO> list){
        if(!CollectionUtils.isEmpty(list)){
            List<String> cardNoList = list.stream().map(MemberCardSellRelationWebDTO::getCardNo).distinct().collect(Collectors.toList());
            Map<String, MemberCard> cardMap = memberCardService.getMapList(cardNoList);
            list.forEach(memberCardSellRelationWebDTO -> {
                MemberCard memberCard = cardMap.get(memberCardSellRelationWebDTO.getCardNo());
                if(null != memberCard){
                    //取订单的价格，并不一定是会员卡的销售价
                    MemberCardEnableOrder memberCardEnableOrder = memberCardEnableOrderService.getOne(new QueryWrapper<MemberCardEnableOrder>().eq(MemberCardEnableOrder.ORDER_NO,memberCardSellRelationWebDTO.getOrderNo()));
                    if (null == memberCardEnableOrder ||null ==memberCardEnableOrder.getPayValue()){
                        memberCardSellRelationWebDTO.setOrderPrice(memberCard.getSalePrice());
                        if(memberCard.getSalePrice() == null){
                            memberCardSellRelationWebDTO.setOrderPrice(0);
                        }
                    }else {
                        memberCardSellRelationWebDTO.setOrderPrice(memberCardEnableOrder.getPayValue());
                    }

                    AccountInfo account = accountInfoService.getOne(new QueryWrapper<AccountInfo>().eq(AccountInfo.ID, memberCard.getSalerId()));
                    if(null != account){
                        memberCardSellRelationWebDTO.setSelaName(account.getUsername());
                    }
                }
            });
        }
    }

}
